package com.example.test.nettytest.netty;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Sharable
public class ConnectorIdleStateTrigger extends ChannelHandlerAdapter {
    private static Logger log = LoggerFactory.getLogger(ConnectorIdleStateTrigger.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.WRITER_IDLE) {
                //4字节的头部  2字节的长度  后面就是内容
                String q = "FF06000000080103"+"EFFF"+"FFEF";
                byte[] sbuf = CRC16M.getSendBuf(q);
                ctx.writeAndFlush(sbuf);
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
